package tech.eyao.hs60x.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.eyao.hs60x.cache.AccountCache;
import tech.eyao.hs60x.co.AccountCo;
import tech.eyao.hs60x.common.CommonConstants;
import tech.eyao.hs60x.common.ConstantsKey;
import tech.eyao.hs60x.enums.Delete;
import tech.eyao.hs60x.manager.IdGenService;
import tech.eyao.hs60x.mapper.AccountDOMapper;
import tech.eyao.hs60x.model.domain.AccountDO;

@Repository
public class AccountDao {
    @Autowired
    private AccountDOMapper accountDOMapper = null;
    @Autowired
    private AccountCache    accountCache    = null;
    @Autowired
    private IdGenService    idGenService    = null;

    public boolean deleteByPrimaryKey(Long id) {
        return accountDOMapper.deleteByPrimaryKey(id) > 0;
    }

    public boolean insert(AccountDO accountDO) throws Exception {
        accountDO.setId(idGenService.nextId());
        accountDO.setGmtCreated(new Date());
        accountDO.setGmtModified(new Date());
        accountDO.setIsDeleted(Delete.N.getValue());
        accountDO.setCreator(CommonConstants.SYSTEM_USER);
        accountDO.setModifier(CommonConstants.SYSTEM_USER);
        boolean result = accountDOMapper.insert(accountDO) > 0;
        // 缓存数据
        if (result) {
            AccountCo accountCo = new AccountCo();
            accountCo.accountType = accountDO.getAccountType();
            accountCo.headUrl = accountDO.getHeadUrl();
            accountCo.phone = accountDO.getPhone();
            accountCo.nickName = accountDO.getNickName();
            accountCache.put(ConstantsKey.getAccountKey(accountDO.getId()), accountCo);
        }
        return result;
    }

    public AccountDO selectByPrimaryKey(Long id) {
        return accountDOMapper.selectByPrimaryKey(id);
    }
    /**
     * 账户信息更新操作
     * @param accountDO
     * @return
     */
    public boolean updateByPrimaryKey(AccountDO accountDO) {
        accountDO.setGmtModified(new Date());
        boolean result = accountDOMapper.updateByPrimaryKey(accountDO) > 0;
        // 缓存数据
        if (result && null != accountCache.get(ConstantsKey.getAccountKey(accountDO.getId()))) {
            AccountCo accountCo = new AccountCo();
            if(null != accountDO.getBirthday()) {
            	accountCo.birthday = accountDO.getBirthday();
            }
            if(StringUtils.isNotEmpty(accountDO.getHeadUrl())) {
            	accountCo.headUrl = accountDO.getHeadUrl();
            }
            if(accountDO.getHeight() > 0) {
            	accountCo.height = accountDO.getHeight();
            }
            if(StringUtils.isNotEmpty(accountDO.getMail())) {
            	accountCo.mail = accountDO.getMail();
            }
            if(org.apache.commons.lang3.StringUtils.isNotEmpty(accountDO.getPhone())) {
            	accountCo.phone = accountDO.getPhone();
            }
            if(null != accountDO.getSex() && accountDO.getSex() > 0) {
            	accountCo.sex = accountDO.getSex();
            }
            
            if(accountDO.getWeight() > 0) {
            	accountCo.weight = accountDO.getWeight();
            }
            accountCache.put(ConstantsKey.getAccountKey(accountDO.getId()), accountCo);
        }
        return result;
    }

    public AccountDO getByPhone(String phone) {
        return accountDOMapper.getByPhone(phone);
    }

    public AccountDO getAccountById(long userId) {
        return accountDOMapper.selectByPrimaryKey(userId);
    }

    /**
     * 查询手表绑定用户
     * 
     * @param userId
     * @param accountId
     * @return
     */
    public boolean isBindWatch(long userId, long accountId) {
        return false;
    }
    
    /**
     * 手表专用
     * @param deviceId
     * @return
     */
    public AccountDO getByDeviceId(String deviceId) {
        return accountDOMapper.getByDeviceId(deviceId);
    }

    /**
     * 批量查询账户详情
     * 
     * @param accountIds
     * @return
     */
    public List<AccountDO> getAccountByListId(List<Long> ids) {
        return accountDOMapper.getAccountByListId(ids);
    }
    /**
     * 查询设备账户信息
     * @param phone
     * @param deviceImei
     * @return
     */
    public AccountDO queryAccoutByDeviceParams(AccountDO accountDO) {
        return accountDOMapper.queryAccoutByDeviceParams(accountDO);
    }
}
